function popup(target) {
	$('fade').className = 'block';
	$('popup-'+target).style.display = 'block';
	if($('popup-'+target).getElement('input')) {
		$('popup-'+target).getElement('input').focus();
	}
}

function close_popup() {
	if($('fade').className == 'block')
		$('fade').className = 'invisible';
	if(document.getElement('div.popup')) {
		var popper = document.getElement('div.popup');
		if(popper.style.display == 'block') {
			popper.style.display = 'none';
		}
	}
}

function forgotPass() {
	$('user-login-form').className = 'invisible';
	$('forgot-pass-form').className = 'visible';
}

function cancelForgotPass() {
	$('user-login-form').className = 'visible';
	$('forgot-pass-form').className = 'invisible';
}

function editInput(target,caller) {
	if($(target)) {
		var temp = $(target).innerHTML;
		$(target).destroy();
		var input = new Element('input', {
			'type': 'text',
		    'value': temp
		});
		input.inject($(caller),'before');
		input.focus();
		input.select();
		$(caller).href = '#';
		$(caller).innerHTML = 'done';
	}
}

window.addEvent('domready', function() {
	if($('parser-update-data') && $('parser-update')) {
		updateAllFeeds();
	}
});

function updateAllFeeds() {
	var group_ids = JSON.decode($('parser-update-data').innerHTML);
	$('parser-update').innerHTML = '<span id="parser-request">Updating feeds...</span>';
	$('parser-update').className = 'visible';
	
	$each(group_ids, function(value) {
		updateGroupFeeds(value);
	});
}

function updateGroupFeeds(target,group_id) {
	var r = new Request( {
		method : 'get',
		url : '/scheduledparser.php?g='+group_id,
		onRequest: function() {
			if($(target)) {
				$(target).disabled = true;
				$(target).value = 'Updating...';
			}
		},
		onComplete : function(response) {
			var data = JSON.decode(response);
			if($(target)) {
				$(target).disabled = false;
				if(data.totals == 0) {
					$(target).value = 'Feeds are up-to-date. Try again.';
				} else {
					$(target).value = 'Updates found... refreshing page.';
					window.location.reload();
				}
			}
		}
	}).send();
}